package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * @author Hlj
 */
@Service(interfaceClass = FoodService.class )
@Transactional
public class FoodServiceImpl implements FoodService{
    @Autowired
    private FoodDao foodDao;
    @Override
    public void upload(List<Food> foods) {
        if (foods != null && foods.size() >0){
            for (Food food : foods) {
                int count = foodDao.findCountByFId(food.getFoodId());
                if (count>0){
                    //此时说明这个id已存在，执行编辑操作
                    foodDao.edit(food);
                } else {
                    foodDao.add(food);
                }
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //每页条数
        Integer pageSize = queryPageBean.getPageSize();
        //页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //查询条件
        String queryString = queryPageBean.getQueryString();
        //分页插件，会在执行sql之前将分页关键字追加到SQL后面
        PageHelper.startPage(currentPage,pageSize);

        Page<Food> page = foodDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Food findById(Integer id) {
        return foodDao.findById(id);
    }

    @Override
    public void add(Food food) {
        foodDao.add(food);
    }

    @Override
    public void edit(Food food) {
        foodDao.edit(food);
    }

    @Override
    public void delete(Integer id) {
        foodDao.delete(id);
    }


}
